<?php

##################################################
#
# Copyright (c) 2008 - 2009 ABSoft Company
# Written and Designed by Dang Tin Trung
#
# This file is part of ABSoft Framework
#
##################################################

if (!defined('EXPONENT')) exit('');

/**
 * Ajax Select File Control
 *
 * @author Dang Tin Trung
 * @copyright 2008 - 2009 ABSoft Company
 * @version 1.0
 * 
 * @package Subsystems
 * @subpackage Forms
 */

/**
 * Manually include the class file for formcontrol, for PHP4
 * (This does not adversely affect PHP5)
 */
require_once(BASE."subsystems/forms/controls/formcontrol.php");

/**
 * Ajax Select File Control
 *
 * @package Subsystems
 * @subpackage Forms
 */
class ajax_select_file extends formcontrol {
	function name() { return "Ajax Select File Control"; }
	function isSimpleControl() { return true; }
	function getFieldDefinition() {
                return array(
			DB_FIELD_TYPE=>DB_DEF_STRING,
			DB_FIELD_LEN=>250,
		);
        }
	
	function ajax_select_file($id_name = "fileupload", $collection_id = 0, $file_id= -1, $load_js = false) {
		$this->load_js = $load_js;
		$this->id_name = $id_name;
		$this->collection_id = $collection_id;
		$this->file_id = $file_id;
	}
	
	function onRegister(&$form) {
		$form->enctype = "multipart/form-data";
	}

	function controlToHTML($name) {
		$html='';
        $lang = exponent_lang_loadFile("datatypes/ajax_select_file.php");
		global $db;
		// get file name
		if ($this->file_id != -1)
		{
      $temp=$db->selectObject('file','id = '.$this->file_id);
		  if ($temp)
		    {
		      $status = $lang['selected_file'] . $temp->filename;
        }
      else
      {
        $status=$lang['no_file_up'];
        $this->file_id = -1;
      }
    }
    else
      $status=$lang['no_file_up'];
    if ($this->load_js)
		  // init script
		  $html.="<script language='Javascript'>
				var ajax_id_selected = '';
        function efm_pickedFile(id, filepath)
		{
			var upload_control = document.getElementById(ajax_id_selected);
			upload_control.value=id;
			upload_control = document.getElementById(ajax_id_selected+'_divname');
			upload_control.innerHTML = '".$lang['selected_file_on']."' + filepath;
		}
		function ajax_select_file_reset(id)
		{
			var upload_control = document.getElementById(id + '_divname');
			upload_control.innerHTML = '".$lang['cleared']."';
			upload_control = document.getElementById(id);
			upload_control.value=-1;
		}
		
		function ajax_select_file(name,id)
		{
          ajax_id_selected = name;
          window.open('".PATH_RELATIVE."modules/filemanagermodule/actions/picker.php?id='+id);
        }
        </script>";
		
		// build fake file ID. With value = -1, we understand that file has not uploaded
		$html.="<input id='".$name."' type=hidden name='".$name."' value='".$this->file_id."'>";
		$html.="<div>
				<div>
					<b>".$lang['status'].":&nbsp;</b>
					<span id='".$this->id_name."_divname'>".$status."</span>
				</div>
				<a id='".$this->id_name."_s'  href='#' onclick=\"javascript:ajax_select_file('".$this->id_name."',".$this->collection_id.");\">".$lang['select_uploaded']."</a>
				&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<a id='clear_".$this->id_name."_s' href='#' onclick=\"ajax_select_file_reset('".$this->id_name."')\">".$lang['clear']."</a>
		</div>";
		return $html;
	}

	function form($object) {
                if (!defined("SYS_FORMS")) require_once(BASE."subsystems/forms.php");
                exponent_forms_initialize();

                $form = new form();
                if (!isset($object->identifier)) {
                        $object->identifier = "";
                        $object->caption = "";
                        $object->default = "";
                }
                $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');

                $form->register("identifier",$i18n['identifier'],new textcontrol($object->identifier));
                $form->register("caption",$i18n['caption'], new textcontrol($object->caption));
                $form->register("default",$i18n['default'], new textcontrol($object->default));
                $form->register("submit","",new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
                return $form;
        }

	function update($values, $object) {
                if ($object == null) $object = new uploadcontrol();
                if ($values['identifier'] == "") {
                        $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');
                        $post = $_POST;
                        $post['_formError'] = $i18n['id_req'];
                        exponent_sessions_set("last_POST",$post);
                        return null;
                }
                $object->identifier = $values['identifier'];
                $object->caption = $values['caption'];
                $object->default = $values['default'];
                return $object;
        }

	function parseData($original_name,$formvalues) {
		$file = $formvalues[$original_name];
		return '<a href="'.URL_FULL.$file.'">'.basename($file).'</a>';
	}
}

?>
